//
//  MainDetailedPageViewController.m
//  时尚男士
//
//  Created by dllo on 15/6/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MainDetailedPageViewController.h"
#import "UIImageView+WebCache.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height self.view.frame.size.height

@interface MainDetailedPageViewController ()
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)UILabel *myLable;
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, retain)NSMutableArray *imgArr;
@property (nonatomic, retain)NSMutableArray *strArr;
@property (nonatomic , retain)UIImageView *imageView1;
@property (nonatomic , retain)UIImageView *imageView2;
@property (nonatomic , retain)UIImageView *imageView3;
@property (nonatomic , retain)UIImageView *imageView4;
@property (nonatomic, retain)UILabel *lable;
@property (nonatomic, retain) NSString *str1;
@property (nonatomic, retain)NSMutableArray *neirongArr;
@property (nonatomic, assign)CGFloat Myheight;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic,retain) NSMutableArray *collectArr ;
@property (nonatomic, retain)NSMutableDictionary *dic;
@property (nonatomic, retain)NSMutableArray *collectTitleArr;
@property (nonatomic,retain)NSString *str;
@end

@implementation MainDetailedPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.neirongArr = [NSMutableArray array];
    // NSLog(@"%@",self.idStr);
    
    self.str = [NSString stringWithFormat:@"http://rm.app.isee.com.cn/json_rm.php?action=detail&albumId=%@&album_type=0&appCookie=channel_AppStore%20version_6.6%20oid_9342E03B-D33F-4832-B1A0-037604E0A97E%20package_com.aslibra.rm.nanren%20platform_iphone%20ver_7.1.2%20jb_1%20t1_1433659733%20md5_b2%20t_1432890077",self.idStr];
      self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barButtonAction:)]autorelease];
    //NSLog(@"%@",str);
    if (self.myIdStr != nil) {
        [self connectionStr:self.myIdStr];
    }else{
        [self connectionStr:self.str];
    }
    
    [self myLayout];
    // Do any additional setup after loading the view.
}
    - (void)barButtonAction:(UIButton *)mybutton{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *myPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"collect.plist"]];
    NSString *myPath1 = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"title.plist"]];
    self.collectArr = [NSMutableArray arrayWithContentsOfFile:myPath];
    self.collectTitleArr = [NSMutableArray arrayWithContentsOfFile:myPath1];
    if (self.collectArr == nil) {
        self.collectArr = [NSMutableArray array];
            }
    if (self.collectTitleArr == nil) {
        self.collectTitleArr = [NSMutableArray array];

    }
    [self.collectArr addObject:self.str];
    if (self.str1 != nil) {
        [self.collectTitleArr addObject:self.str1];
    }
    
    [self.collectTitleArr writeToFile:myPath1 atomically:YES];
    [self.collectArr writeToFile:myPath atomically:YES];
    NSLog(@"%@",myPath);
                        
}
-(void)myLayout{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width ,Height - 50)];
    [self.view addSubview:self.scrollView];
         
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, 200)];
    [ self.scrollView addSubview:self.imageView];
    self.imageView.image = [UIImage imageNamed:@"reload.jpg"];
    
     self.scrollView.contentSize = CGSizeMake(0, 950);
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, Width, 40)];
    [ self.scrollView addSubview:self.label];
    
    self.myLable = [[UILabel alloc] initWithFrame:CGRectZero];
    [ self.scrollView addSubview:self.myLable];
    self.myLable.numberOfLines = 0;
    //self.myLable.adjustsFontSizeToFitWidth = YES;
    self.imageView1= [[UIImageView alloc] initWithFrame:CGRectZero];
    
    self.imageView1.image = [UIImage imageNamed:@"reload.jpg"];
    [ self.scrollView addSubview: self.imageView1];
    
    self.imageView2= [[UIImageView alloc] initWithFrame:CGRectZero];
    
    self.imageView2.image = [UIImage imageNamed:@"reload.jpg"];
    [ self.scrollView addSubview: self.imageView2];
    
    self.imageView3= [[UIImageView alloc] initWithFrame:CGRectMake(Width /3 * 2 + 4 , 360, Width / 3 - 4 , 200)];
    
    self.imageView3.image = [UIImage imageNamed:@"reload.jpg"];
    [ self.scrollView addSubview: self.imageView3];
    
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectZero];
    [ self.scrollView addSubview: self.lable];
    self.lable.numberOfLines = 0;
    self.lable.adjustsFontSizeToFitWidth = YES;
    //self.lable.text = @"深秋和冬夏.羽绒服是男士御寒的必须品,一款适合自身职业与生活的羽绒服才是男人的需求.彰显气质而又不失时尚!保暖而不能臃肿,职业而又能休闲";
    
    [ self.lable release];
    
    self.imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 700, Width,Width / 292 * 220)];
    self.imageView4.image = [UIImage imageNamed:@"reload.jpg"];
    [ self.scrollView addSubview:self.imageView4];
    
    [ self.imageView1 release];
    [self.imageView release];
    [ self.label release];
    [ self.myLable release];
    [ self.scrollView release];
}
-(void)DataprocessingDic:(id)myData{
    self.dic = [NSMutableDictionary dictionary];
    self.dic = myData;
    self.imgArr = [NSMutableArray array];
    NSMutableArray *arr = [myData objectForKey:@"data"];
    for (NSMutableDictionary *dic in arr) {
        if ([[dic objectForKey:@"product"] isKindOfClass:[NSArray class]]) {
            NSMutableArray *arr1 = [dic objectForKey:@"product"];
            for (NSMutableDictionary *dic1 in arr1) {
                if (!([[dic1 objectForKey:@"img"] isEqualToString:@""])) {
                    NSString *img = [dic1 objectForKey:@"img"];
                    [self.imgArr addObject:img];
                }
                
                
            }
        }
        
        if ([[dic objectForKey:@"block"] isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *mydic1 = [dic objectForKey:@"block"];
            
            if ( ![[mydic1 objectForKey:@"article"] isEqualToString:@""]) {
                NSString *str2 = [mydic1 objectForKey:@"article"];
                
                [self.neirongArr addObject:str2];
            }
            
            if ( (![[mydic1 objectForKey:@"title"] isEqualToString:@""]) && self.str1 == nil) {
                self.str1 = [mydic1 objectForKey:@"title"];
                self.str1 = [self.str1 stringByReplacingOccurrencesOfString:@"&mdash;" withString:@"-"];
                self.label.text = self.str1;
            }
            
        }
    }
    if (!self.imgArr.count == 0) {
        NSLog(@"%lu",(unsigned long)self.imgArr.count);
        NSURL *url = [NSURL URLWithString:[self.imgArr objectAtIndex:0]];
        [self.imageView setImageWithURL:url];
        url = [NSURL URLWithString:[self.imgArr objectAtIndex:1]];
        [self.imageView1 setImageWithURL:url];
        
        url = [NSURL URLWithString:[self.imgArr objectAtIndex:2]];
        [self.imageView2 setImageWithURL:url];
        url = [NSURL URLWithString:[self.imgArr objectAtIndex:3]];
        [self.imageView3 setImageWithURL:url];
        url = [NSURL URLWithString:[self.imgArr objectAtIndex:4]];
        [self.imageView4 setImageWithURL:url];
    }
    if (self.neirongArr.count != 0 && self.neirongArr.count >= 2) {
        self.myLable.text = [self.neirongArr objectAtIndex:0];
        CGSize size = [self sizeWithString:self.myLable.text font:self.myLable.font];
        self.myLable.frame = CGRectMake(10, 240, size.width, size.height);
        self.imageView1.frame = CGRectMake(0, 250 + size.height, Width / 3 - 4, 200);
        self.imageView2.frame = CGRectMake (Width / 3 + 2 ,250 + size.height, Width / 3 - 4,200);
        self.imageView3.frame = CGRectMake(Width /3 * 2 + 4 ,250 + size.height , Width / 3 - 4 , 200);
        if (![[self.neirongArr objectAtIndex:1] hasPrefix:@"h"]) {
            self.lable.text = [self.neirongArr objectAtIndex:1];
            CGSize mysize = [self sizeWithString:self.label.text font:self.label.font];
            self.label.frame = CGRectMake(10, 260 + size.height + 200, Width - 20, mysize.height);
            self.imageView4.frame = CGRectMake(0, 470 +size.height + mysize.height, Width, Width / 292 * 220);
        }if ([[self.neirongArr objectAtIndex:1] hasPrefix:@"h"]) {
            self.imageView4.frame = CGRectMake(0, 470 + size.height, Width,  Width / 292 * 220);
            self.scrollView.contentSize = CGSizeMake(0, 980);
        }
        
    }
    
    if (!(self.neirongArr.count != 0 && self.neirongArr.count >= 2)) {
        
        self.imageView1.frame = CGRectMake(0, 240 , Width / 3 - 4, 200);
        self.imageView2.frame = CGRectMake (Width / 3 + 2 ,2400, Width / 3 - 4,200);
        self.imageView3.frame = CGRectMake(Width /3 * 2 + 4 ,2400 , Width / 3 - 4 , 200);
        self.imageView4.frame = CGRectMake(0, 440, Width, Width / 292 * 220);
        self.scrollView.contentSize = CGSizeMake(0, 700);
    }
    
    
}

- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(Width - 20, Height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
